<template>
	<view>
		<view class="cu-card case no-card">
			<view class="cu-item shadow">
				<view class="image">
					<image src="https://ossweb-img.qq.com/images/lol/web201310/skin/big10006.jpg" mode="widthFix">
					</image>
				</view>
				<view class="cu-list menu-avatar">
					<view class="cu-item">
						<view class="cu-avatar round lg"
						      :style="{ 'background-image': 'url(' + (userData.avatar ? userData.avatar : 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10006.jpg') + ')' }">
						</view>
						<view class="content flex-sub">
							<view class="text-grey" v-if="isLogin">
								{{ statusMap[userData.roleId] }}
								{{ userData.account }}
							</view>
							<view class="text-grey" v-else @click="toLogin">
								一键登录
							</view>
							
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="cu-form-group">
			<view class="text-light" @click="toAuth">实名认证</view>
		</view>
		<view class="cu-form-group" v-if="userData.roleId == 2">
			<view class="text-light" @click="toApplyDriver">成为司机</view>
		</view>
		<view class="cu-form-group">
			<view class="text-light" @click="exitLogin">退出登录</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLogin: true,
				statusMap: {
					1: "用户",
					2: "司机"
				},
				userData: {
					account: "",
					roleId: "",
					avatar:null
				}
			}
		},
		methods: {
			/* 退出登录的方法 */
			exitLogin(){
				localStorage.removeItem("token");
				localStorage.removeItem("user");
				uni.reLaunch({
				    url: '/pages/userCenter/userCenter' // 需要刷新的页面路径
				});
			},
			/* 实名认证的方法 */
			toAuth(){
				//先判断有没有用户登录
				if(this.isLogin){
					uni.navigateTo({
						//路径
						url: '/pages/userAuth/userAuth'
					});
				}else{
					uni.showToast({
						title:"请先登录！",icon:"error"
					})
				}
			
			},
			reBack() {
				uni.navigateBack()
			},
			toApplyDriver() {
				//前往司机申请页面
				uni.navigateTo({
					//路径
					url: '/pages/driver-apply/driver-apply'
				});
			},
			toLogin() {
				//前往登录页面
				uni.navigateTo({
					//路径
					url: '/pages/signIn/login'
				});

			},
			checkLogin() {
				//查验是否有用户登录
				var user = localStorage.getItem("user");
				if (user == null || user == '') {
					//用户未登录
					this.isLogin = false;
					this.userData = {};
				} else {
					//用户信息获取
					this.userData = JSON.parse(user)
				}
			}
		},
		created() {
			this.checkLogin()
		}
	}
</script>

<style>

</style>